<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML到DOCX转换测试 - 样式继承</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            line-height: 1.6;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            color: #333;
        }
        h1 {
            color: #2c3e50;
            border-bottom: 2px solid #3498db;
            padding-bottom: 10px;
        }
        textarea {
            width: 100%;
            height: 300px;
            padding: 10px;
            font-family: monospace;
            font-size: 14px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
        }
        button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            margin: 10px 0;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #2980b9;
        }
        #status {
            margin-top: 10px;
            padding: 10px;
            border-radius: 4px;
        }
        #warnings {
            margin-top: 10px;
            padding: 10px;
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            border-radius: 4px;
            display: none;
        }
    </style>
</head>
<body>
    <h1>HTML到DOCX转换测试 - 样式继承</h1>
    
    <p>这个测试页面可以帮助您验证HTML中的CSS样式（特别是颜色）是否能正确地继承到转换后的DOCX文档中。</p>
    
    <div>
        <label for="html-input">HTML内容：</label>
        <textarea id="html-input"><!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
        body {
            color: red;
            font-size: 16px;
            font-family: "微软雅黑";
            margin: 0;
            max-width: 21cm;
            padding: 2.54cm;
        }
        
        p {
            margin: 0;
        }
    </style>
</head>
<body>
    <div>
        打火机手动滑稽阿萨德黄金卡
    </div>
</body>
</html></textarea>
    </div>
    
    <button id="convert-btn">转换并下载 DOCX</button>
    
    <div id="status"></div>
    
    <div id="warnings"></div>
    
    <script>
        const convertBtn = document.getElementById('convert-btn');
        const htmlInput = document.getElementById('html-input');
        const statusDiv = document.getElementById('status');
        const warningsDiv = document.getElementById('warnings');
        
        // 向服务器发送转换请求
        async function convertHtmlToDocx(htmlContent) {
            try {
                // 向服务器发送POST请求
                const response = await fetch('/convert', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ html: htmlContent })
                });
                
                if (!response.ok) {
                    throw new Error(`服务器响应错误: ${response.statusText}`);
                }
                
                // 获取文件数据
                const blob = await response.blob();
                
                // 创建下载链接
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = `测试文档-${new Date().getTime()}.docx`;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                window.URL.revokeObjectURL(url);
                
                return { warnings: [] };
            } catch (error) {
                throw new Error(`转换失败: ${error.message || '未知错误'}`);
            }
        }
        
        convertBtn.addEventListener('click', async () => {
            convertBtn.disabled = true;
            convertBtn.textContent = '转换中...';
            statusDiv.textContent = '';
            warningsDiv.style.display = 'none';
            
            try {
                const htmlContent = htmlInput.value;
                statusDiv.textContent = '正在转换HTML到DOCX...';
                
                // 在实际环境中，这里应该使用真实的htmlToDocx函数
                // 由于演示限制，我们使用模拟的转换函数
                const { buffer, warnings } = await convertHtmlToDocx(htmlContent);
                
                // 显示警告信息
                if (warnings.length > 0) {
                    warningsDiv.innerHTML = `
                        <h4 style="margin-top: 0; color: #856404;">转换警告:</h4>
                        <ul style="margin-bottom: 0;">
                            ${warnings.map(warning => `<li>${warning}</li>`).join('')}
                        </ul>
                    `;
                    warningsDiv.style.display = 'block';
                }
                
                // 创建下载链接
                const blob = new Blob([buffer], {
                    type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                });
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = `测试文档-${new Date().getTime()}.docx`;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                window.URL.revokeObjectURL(url);
                
                statusDiv.innerHTML = '<span style="color: #28a745;">转换成功！文件已开始下载。请在Word中打开查看样式继承效果。</span>';
            } catch (error) {
                console.error('转换失败:', error);
                statusDiv.innerHTML = `<span style="color: red;">转换失败: ${error.message || '未知错误'}</span>`;
            } finally {
                convertBtn.disabled = false;
                convertBtn.textContent = '转换并下载 DOCX';
            }
        });
    </script>
</body>
</html>